package com.liaozibo.demo.seckill.support.service.impl;

import com.liaozibo.demo.seckill.support.common.exception.BizException;
import com.liaozibo.demo.seckill.support.dao.mapper.ActivityMapper;
import com.liaozibo.demo.seckill.support.dao.mapper.ProductMapper;
import com.liaozibo.demo.seckill.support.dao.model.ActivityDO;
import com.liaozibo.demo.seckill.support.dao.model.ProductDO;
import com.liaozibo.demo.seckill.support.export.dto.ActivityDTO;
import com.liaozibo.demo.seckill.support.service.ActivityService;
import com.liaozibo.demo.seckill.support.util.RedisUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class ActivityServiceImpl implements ActivityService {
    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private RedisUtils redisUtils;

    @Override
    public ActivityDTO getActivityById(Integer id) {
        ActivityDO activityDO = activityMapper.selectById(id);
        ActivityDTO activityDTO = new ActivityDTO();
        BeanUtils.copyProperties(activityDO, activityDTO);
        return activityDTO;
    }

    @Override
    public ActivityDTO getActivityByCode(String code) {
        ActivityDO activityDO = activityMapper.selectByCondition(code, null);
        if (activityDO == null) {
            return null;
        }
        ActivityDTO activityDTO = new ActivityDTO();
        BeanUtils.copyProperties(activityDO, activityDTO);
        return activityDTO;
    }

    @Override
    public ActivityDTO getActivityByCondition(String code, Integer status) {
        return null;
    }

    @Override
    public void createActivity(ActivityDTO activityDTO) {
        ProductDO productDO = productMapper.selectByCode(activityDTO.getCode());
        if (productDO == null) {
            throw new BizException("商品不存在");
        }
        ActivityDO existActivity = activityMapper.selectByCondition(activityDTO.getCode(), null);
        if (existActivity != null && existActivity.getStatus() != ActivityDTO.STATUS_END) {
            throw new BizException("活动已存在");
        }
        ActivityDO activityDO = new ActivityDO();
        BeanUtils.copyProperties(activityDTO, activityDO);
        activityMapper.insert(activityDO);
    }

    /**
     * 更新活动状态和商品标识
     * */
    @Override
    @Transactional
    public void startActivity(String code) {
        ActivityDO activityDO = activityMapper.selectByCondition(code, 0);
        if (activityDO == null) {
            throw new BizException("商品没有未开始活动");
        }
        LocalDateTime now = LocalDateTime.now();
        if (now.isBefore(activityDO.getStartTime())) {
            throw new BizException("未到活动开始时间");
        }
        if (now.isAfter(activityDO.getEndTime())) {
            throw new BizException("活动已过期");
        }
        activityMapper.updateStatus(activityDO.getId(), 1);
        productMapper.updateTag(activityDO.getCode(), 1);
        redisUtils.set("stock_" + code, String.valueOf(activityDO.getStock()));
    }

    /**
     * 更新活动状态和商品标识
     * */
    @Override
    @Transactional
    public void endActivity(String code) {
        ActivityDO activityDO = activityMapper.selectByCondition(code, 1);
        if (activityDO == null) {
            throw new BizException("商品没有正在进行中的活动");
        }
        activityMapper.updateStatus(activityDO.getId(), 2);
        productMapper.updateTag(activityDO.getCode(), 0);
    }

    @Override
    public void decrementStock(String code, Integer num) {}
}
